package com.haixiaoke.saas.lock.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.saas.common.config.DevicesConfig;
import com.haixiaoke.saas.lock.domain.DeviceLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能锁平台接口
 *
 * @author qixi
 * @date 2024/7/29
 */
@Service
public class LockDevicesService {

    @Autowired
    private DevicesConfig devicesConfig;

    /**
     * 新增智能锁
     *
     * @param deviceLock
     */
    public void createSmartLock(DeviceLock deviceLock) {
        // 调用设备配置中心接口创建房间锁
        Map<String, Object> map = new HashMap<>();
        map.put("brandId", deviceLock.getBrandId());
        map.put("brandName", deviceLock.getBrandName());
        map.put("lockName", deviceLock.getLockName());
        map.put("modelId", deviceLock.getModelId());
        map.put("modelName", deviceLock.getModelName());
        map.put("sn", deviceLock.getLockMac());
        devicesConfig.sendRequest(RequestMethod.POST, map, "/business/lock");
    }

    /**
     * 新增门禁人脸
     *
     * @param img
     * @param mac
     * @param identification
     */
    public void createLockFace(String img, String mac, String identification,Date startTime, Date endTime) {
        Map<String, Object> lockFaceMap = new HashMap<>();
        lockFaceMap.put("faceImage", img);
        lockFaceMap.put("identification", identification);
        lockFaceMap.put("passwordType", 1);
        lockFaceMap.put("sn", mac);
        lockFaceMap.put("startTime", startTime);
        lockFaceMap.put("endTime", endTime);
        devicesConfig.sendRequest(RequestMethod.POST, lockFaceMap, "/business/face");
    }

    /**
     * 编辑门禁人脸
     *
     * @param img
     * @param mac
     * @param identification
     */
    public void updateLockFace(String img, String mac, String identification, Date startTime, Date endTime) {
        Map<String, Object> lockFaceMap = new HashMap<>();
        lockFaceMap.put("faceImage", img);
        lockFaceMap.put("identification", identification);
        lockFaceMap.put("passwordType", 1);
        lockFaceMap.put("sn", mac);
        lockFaceMap.put("startTime", startTime);
        lockFaceMap.put("endTime", endTime);
        devicesConfig.sendRequest(RequestMethod.PUT, lockFaceMap, "/business/face");
    }


    /**
     * 删除门禁人脸
     *
     * @param identification
     */
    public void removeFace(String mac, String identification) {
        String parameter = "?sn=" + mac + "&identification=" + identification;
        devicesConfig.sendRequest(RequestMethod.DELETE, null, "/business/face/remove" + parameter);
    }


    /**
     * 新增门禁密码
     *
     * @param password
     * @param identification
     */
    public void createLockPwd(Date startTime, Date endTime,String mac, String password, String identification) {
        Map<Object, Object> map = new HashMap<>();
        map.put("sn", mac);
        map.put("password", password);
        map.put("generateType", 1);
        map.put("identification", identification);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        devicesConfig.sendRequest(RequestMethod.POST, map, "/business/password");
    }

    /**
     * 删除门禁密码
     *
     * @param mac
     * @param identification
     */
    public void removeLockPwd(String mac, String identification) {
        String parameter = "?sn=" + mac + "&identification=" + identification;
        devicesConfig.sendRequest(RequestMethod.DELETE, null, "/business/password/remove" + parameter);
    }

    public void removeLock(String mac) {
        devicesConfig.sendRequest(RequestMethod.DELETE, null, "/business/lock/sn/" + mac);
    }

    public String getIdentification() {
        return DateUtil.format(DateUtil.date(), "yyyyMMdd") + RandomUtil.randomNumbers(8);
    }

    public JSONObject selectListBySn(List<String> sns) {
        return devicesConfig.sendRequest(RequestMethod.POST, sns, "/business/lock/listBySns");
    }

    /**
     * 查询第三方智能锁信息
     */
    public JSONObject selectListAll(Map<String, Object> map) {
        return devicesConfig.sendRequest(RequestMethod.POST, map, "/business/lock/listAll");
    }
}
